/**
* @author Ucar Sahin
*
* 31.05.2011
* Login.java
* 
*/

package at.fhv.easyDietWeb.client.loginService;

import at.fhv.easyDietWeb.client.protocolService.ProtocolPanel;
import at.fhv.easyDietWeb.shared.util.wrappers.UserWrapper;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point for the login to the EasyDietWeb
 * @author Sahin
 *
 */
public class Login implements EntryPoint
{
	private Label _usernameLabel;
	private Label _pwdLabel;
	private TextBox _usernameField;
	private PasswordTextBox _pwdField;
	private Button _loginButton;
	private DialogBox _loginBox;
	private Label _errorLabel;
	private UserWrapper _user;
	private ILoginServiceAsync ls;

	@Override
	public void onModuleLoad()
	{
		ls = GWT.create(ILoginService.class);
		
		ls.checkSession(new AsyncCallback<UserWrapper>() {
			
			@Override
			public void onSuccess(UserWrapper result)
			{
				if (result == null)
				{
					_usernameLabel = new Label("Benutzername");
					_usernameField = new TextBox();
					_pwdLabel = new Label("Kennwort");
					_pwdField = new PasswordTextBox();
					_loginButton = new Button("login");
					_loginBox = createLoginBox();
					
					_loginBox.setGlassEnabled(true);
			        _loginBox.setAnimationEnabled(true);
			        _loginBox.setModal(true);
			        _loginBox.center();
			        _loginBox.show();
				}
				else
				{
					_user = result;
					loadWelcomeView();
				}
			}
			
			@Override
			public void onFailure(Throwable caught)
			{
				DialogBox db = new DialogBox();
				db.setTitle("Server Error");
				VerticalPanel dialogLayout = new VerticalPanel();
				dialogLayout.setSpacing(5);
				db.add(dialogLayout);
				
				FlexTable ft = new FlexTable();
				ft.setHTML(0, 0, "verbindungsaufbau zum server gescheitert! bitte ueberpruefen sie ihre"
							   + " Netzwerkverbindung und ueberpruefen sie es erneut!");
				
				db.add(dialogLayout);
			}
		});
		
	}

	/**
	 * Creates a login box
	 * @return the login box
	 */
	private DialogBox createLoginBox()
	{
		DialogBox dB = new DialogBox();
		dB.setText("EasyDiet - Login");
		
		//layout for the loginBox
		VerticalPanel dialogLayout = new VerticalPanel();
		dialogLayout.setSpacing(5);
		dB.setWidget(dialogLayout);
		
		//add labels and fields to the dialogBox
		//init the components to be added
		_usernameLabel = new Label("Benutzername");
		_usernameField = new TextBox();
		_pwdLabel = new Label("Kennwort");
		_pwdField = new PasswordTextBox();
		_loginButton = new Button("login");
		_errorLabel = new Label("");
		
		FlexTable flexTable = new FlexTable();
		flexTable.setCellSpacing(5);
		flexTable.setWidth("300px");
		
		flexTable.setWidget(1, 0, _usernameLabel);
        flexTable.setWidget(1, 1, _usernameField);
        flexTable.setWidget(2, 0, _pwdLabel);
        flexTable.setWidget(2, 1, _pwdField);
        flexTable.setWidget(3, 1, _errorLabel);
        
        dialogLayout.add(flexTable);
        dialogLayout.setCellHorizontalAlignment(flexTable, HasHorizontalAlignment.ALIGN_CENTER);
		
		_loginButton.addClickHandler(new ClickHandler()
									{			
										@Override
										public void onClick(ClickEvent event)
										{
											validateLogin();											
										}
									});
		dialogLayout.add(_loginButton);
		dialogLayout.setCellHorizontalAlignment(_loginButton, HasHorizontalAlignment.ALIGN_RIGHT);
		
		return dB;
	}
	
	/**
	 * sends the login data to the server to validate them;
	 * if the validation was successful, the user is logged in
	 */
	private void validateLogin()
	{		
		ls.validateLogin(_usernameField.getText(), _pwdField.getText(), new AsyncCallback<UserWrapper>()
			{
				@Override
				public void onFailure(Throwable caught)
				{
					_errorLabel.setText("verbindungsaufbau zum server gescheitert! bitte ueberpruefen sie ihre"
										+ " Netzwerkverbindung und ueberpruefen sie es erneut!");
				}
	
				@Override
				public void onSuccess(UserWrapper result)
				{

					if (result != null)
					{
						_user = result;
						_loginBox.hide();
						loadWelcomeView();
					}
					else
					{
						_errorLabel.setText("ungueltiger benutzername oder kennwort! bitte versuchen sie es erneut!");
					}
				}				
			});
	}

	/**
	 * creates and shows the welcome panel in the browser
	 */
	protected void loadWelcomeView()
	{
		RootPanel.get("naviContainer").clear();
		RootPanel.get("contentContainer").clear();
		RootPanel.get("welcomeLabelContainer").clear();
		
		//get the container for the new ui-compenents
		RootPanel header = RootPanel.get("welcomeLabelContainer");
		RootPanel navi = RootPanel.get("naviContainer");
		//RootPanel content = RootPanel.get("contentContainer");
			
		//create the welcomeLabel-widget
		Label welcomeLabel = new Label("Willkommen " + _user.getLastName() + ", " + _user.getFirstName());
		
		//create the navigation-elements
		final Button homeButton = new Button("HOME");
		final Button npButton = new Button("Protokoll erzeugen");
		
		homeButton.setStyleName("naviButton");
		npButton.setStyleName("naviButton");

		homeButton.addClickHandler(new ClickHandler()
									{			
										@Override
										public void onClick(ClickEvent evt)
										{
											loadWelcomeView();
										}
									});
		
		npButton.addClickHandler(new ClickHandler()
								{			
									@Override
									public void onClick(ClickEvent evt)
									{
										loadNPJ();
									}
								});
		
		//layout for the navigation
		VerticalPanel navigationVPanel = new VerticalPanel();
		navigationVPanel.setSpacing(5);
		navigationVPanel.add(homeButton);
		navigationVPanel.add(npButton);
		
		//content of the welcome view		
		//to be implemented		
		
		//add the created ui-compenents to the rootpanel
		header.add(welcomeLabel);
		navi.add(navigationVPanel);
	}

	/**
	 * loads and shows the form for the nutritional journal
	 */
	private void loadNPJ()
	{		
		RootPanel content = RootPanel.get("contentContainer");
		content.clear();		
		
		ProtocolPanel pp = new ProtocolPanel();
		content.add(pp);
	}
}